//
//  NickNameViewController.m
//  Hai
//
//  Created by gaohaojie on 16/6/17.
//  Copyright © 2016年 wangzijian. All rights reserved.
//

#import "ShowPersonViewController.h"
#import "PersonCell.h"
#import "UIImageView+WebCache.h"
#import "CaptureViewController.h"
#import "SJAvatarBrowser.h"
#import "AddTaskViewController.h"

@interface ShowPersonViewController ()
@property (assign, nonatomic) long flag;
@end

@implementation ShowPersonViewController
{
    NSInteger _buttonIndex;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden =NO;
    self.navigationItem.title = @"选择接受人";
    [self.collectionView registerClass:[PersonCell class] forCellWithReuseIdentifier:@"PersonCell"];
    [self initDate];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
//是否加载底部导航
- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)initDate
{
    _arrTimes = [NSMutableArray new];
    NSArray* times = [[NSArray alloc] initWithObjects:@"大奶奶",@"二姨太",@"三姨太",nil];
    for (NSString *t in times)
    {
        //if (![image isEqualToString:@""]) {
        [_arrTimes addObject:t];
        //}
    }
    [self.collectionView reloadData];
    //    NSString *url=[NSString stringWithFormat:@"%@Loan/GetLoanUploadByType",BaseURL];
    //    NSDictionary *parameters = @{@"loanid":_loanid,@"typeid":@"2",@"signature":[Util getSignature:[NSString stringWithFormat:@"%@2",_loanid]]};
    //    AFHTTPRequestOperation*op= [_manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //        NSError*error;
    //        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
    //        //NSLog(@"%@",dic);
    //        NSNumber *result = [dic valueForKey:@"Status"];
    //        if (error==nil) {
    //            if ([result isEqualToNumber:[[NSNumber alloc] initWithInt:1]]) {
    //                NSData *jsonData = [[dic valueForKey:@"Data"] dataUsingEncoding:NSUTF8StringEncoding];
    //                NSError *err;
    //                NSString *UploadImages = [[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]valueForKey:@"UploadImages"];
    //                NSArray* images = [UploadImages componentsSeparatedByString:@","];
    //                for (NSString *image in images) {
    //                    //     UIImageView * imageView = [[UIImageView alloc]init];
    //                    //      [imageView sd_setImageWithURL:[NSURL URLWithString:image]];
    //                    //      [_imageArray addObject:imageView];
    //                    if (![image isEqualToString:@""]) {
    //                        [_imagePaths addObject:image];
    //                    }
    //                }
    //                [self.collectionView reloadData];
    //
    //            }else if ([result isEqualToNumber:[[NSNumber alloc] initWithInt:0]]){
    //                [[[UIAlertView alloc] initWithTitle: @"发送失败，请稍后再试" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    //
    //                return;
    //            }
    //        }
    //
    //    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //        //NSLog(@"网络异常，error = %@",error.localizedDescription);
    //    }];
    //
    //    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    //    [op start];
}

-(IBAction)save
{
    //    NSString *str = self.rtime.text;
    //    NSLog(@"%@",str);
    AddTaskViewController *user = [[AddTaskViewController alloc]init];
    [self.navigationController pushViewController:user animated:YES];
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 30);
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.arrTimes count];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)cView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonCell * cell =  (PersonCell *)[cView dequeueReusableCellWithReuseIdentifier:@"PersonCell" forIndexPath:indexPath];
    cell.tag  = _flag;
    _flag++;
    //if((long)indexPath.row!=[_imagePaths count]){//如果表格的序号不等于image集合的最大长度
    NSString *image = [_arrTimes objectAtIndex:indexPath.row]; //生产一个应用模块
    //[cell.image sd_setImageWithURL:[NSURL URLWithString:image]];
    // [cell.image setImage:imageView.image];
    cell.timetext.text = [NSString stringWithFormat:@"%@",image];
    cell.xbtn.tag=cell.tag;
    [cell.xbtn addTarget:self action:@selector(xCell:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)xCell:(UIButton *)btn{
    //取出点击的cell
    
    PersonCell *c = (PersonCell * )[_collectionView viewWithTag:btn.tag];
    NSIndexPath *ffIndexPath = [_collectionView indexPathForCell:c];
    NSUInteger didPosition = ffIndexPath.row;
    //  [_imageArray removeObjectAtIndex:didPosition];
    [_arrTimes removeObjectAtIndex:didPosition];
    
    NSMutableArray *indexPathArray = [NSMutableArray array];
    NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:didPosition inSection:0];
    [indexPathArray addObject:tempIndexPath];
    [_collectionView deleteItemsAtIndexPaths:indexPathArray];
}
@end
